package application.searchPatientInfo;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

import application.DaoFactory;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import pojo.Patient;
import utils.EventUtils;

public class SearchPatientInfoByNameEvent implements Initializable {
	@FXML
	private Button searchButton;
	@FXML
	private TextField patientNameTextField;
	@FXML
	private HBox hBox;
	private VBox myVBox;

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		try {
			myVBox = FXMLLoader.load(MyVBoxEvent.class.getResource("MyVBox.fxml"));
			hBox.getChildren().add(myVBox);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// Glyph glyph =EventUtils.fontAwesome.create(FontAwesome.Glyph.SEARCH);
		// searchButton.setGraphic(glyph);
		// TODO Auto-generated method stub

	}

	@FXML
	private void search() {
		String patientName = patientNameTextField.getText().trim();
		TableView<Patient> tableView = (TableView<Patient>) myVBox.lookup("#tableView");
		TableColumn<Patient, String> idColumn = (TableColumn<Patient, String>) tableView.getColumns().get(0);
		TableColumn<Patient, String> nameColumn = (TableColumn<Patient, String>) tableView.getColumns().get(1);
		TableColumn<Patient, String> loginTimeColumn = (TableColumn<Patient, String>) tableView.getColumns().get(2);
		idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
		nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
		loginTimeColumn.setCellValueFactory(new PropertyValueFactory<>("logintime"));
		List<Patient> patientList = DaoFactory.patientMapper.selectLikePatientName(patientName);
		ObservableList<Patient> patients = FXCollections.observableArrayList();
		patients.addAll(patientList);
		tableView.setItems(patients);
	}

}
